/**
 * 
 */
package com.activities;


import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.callbacks.Callbacks.CATCompleted;
import com.clases.Categoria;
import com.constantes.Constantes;
import com.example.santiagotur.R;
import com.listener.MenuListener;


/**
 * @author Chris
 * 
 */
public class MenuActivity extends Fragment {

    private simpleAdapter adapter;

    private ListView lista;

    private View v;

    public class simpleAdapter extends BaseAdapter {

        private final Context context;

        private final ArrayList<Integer> icons = new ArrayList<Integer>();

        private final ArrayList<String> texts = new ArrayList<String>();

        class ViewHolder {

            ImageView icon;

            TextView texto;
        }

        public simpleAdapter(Context context) {

            super();
            this.context = context;
        }

        public void add(String text, Integer icon) {

            texts.add(text);
            icons.add(icon);
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {

            return texts.size();
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {

            // TODO Auto-generated method stub
            return null;
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {

            return icons.get(position);
        }

        /*
         * (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {

                convertView = LayoutInflater.from(context).inflate(R.layout.item_menu_row, null);
                holder = new ViewHolder();
                //
                holder.texto = (TextView) convertView.findViewById(R.id.row_title);
                holder.icon = (ImageView) convertView.findViewById(R.id.row_icon);

                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.texto.setText(texts.get(position));
            // holder.icon.setImageResource(icons.get(position));

            return convertView;

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.categoriaz, null);

        inicializar();

        return v;
    }

    /**
     * 
     */
    private void inicializar() {

        lista = (ListView) v.findViewById(R.id.listCategorias);

        if (adapter == null) {

            adapter = new simpleAdapter(this.getActivity());

            Categoria cat = new Categoria();
            cat.setCallbackCategoria(new CATCompleted() {

                @Override
                public void Completed(Categoria[] result) {

                    procesarInfo(result);

                }
            });
            cat.execute(Constantes.ACCION_CATEGORIA_GETALL);
        }

    }

    private void procesarInfo(Categoria[] result) {

        adapter.add(getActivity().getResources().getString(R.string.categoria_inicio), 0);

        for (Categoria cat : result) {

            adapter.add(cat.getNombreCategoria(), cat.getIDCategoria());
        }

        lista.setAdapter(adapter);
        lista.setOnItemClickListener(new MenuListener(getActivity(), lista));
    }
}
